<?php

namespace App\controller;

use app\common\MsgType;
use App\model\Email;
use App\model\EmailCate;
use App\model\Guild;
use App\model\GuildLevelUp;
use App\model\GuildMember;
use App\model\GuildTask;
use App\model\GuildTaskLog;
use App\model\GuildTechConf;
use App\model\GuildTechnology;
use App\model\Mail;
use App\model\Role;
use app\model\Room;
use App\model\Shop;
use app\model\User;
use GatewayWorker\Lib\Gateway;
use support\Constant;
use support\Request;
use support\Response;
use function Illuminate\Events\queueable;

class GuildController extends CommonController
{
    /**
     * 创建公会
     * @param Request $request
     * @return Response
     */
    public function createGuild(Request $request)
    {
        $data = $request->postData;
        // print_r($data);
        $grade = $data['grade'] ?? 1;
        $uuid = $data['uuid'] ?? '';
        $role_id = $data['role_id'] ?? 0;
        $name = $data['name'] ?? '盟重公会';
        if (empty(($uuid))) {
            return outJson(1, '玩家id为空');
        }
        if (empty(($role_id))) {
            return outJson(1, '角色id为空');
        }
        if (empty($name)) {
            return outJson(1, '公会名称不能为空！');
        }

        //判断玩家是否加入公会
        $where_cond['uuid'] = $uuid;
        $where_cond['role_id'] = $role_id;
        $is_join = GuildMember::query()->where($where_cond)->exists();
        // var_dump($is_join);
        if ($is_join) {
            return outJson(1, '已加入其他公会');
        }

        $chat_model = new Guild();
        $chat_model->grade = $grade;
        $chat_model->uuid = $uuid;
        $chat_model->name = $name;
        $chat_model->role_id = $role_id;
        $chat_model->save();

        $guild_member = new GuildMember();
        $guild_member->guild_id = $chat_model->id;
        $guild_member->uuid = $uuid;
        $guild_member->job = Constant::JOB_1;
        $guild_member->role_id = $role_id;
        $guild_member->save();
        // print_r(3333);

        return outJson(0, '创建成功！');
    }

    /***
     * 公会列表
     * @param Request $request
     * @return Response
     */
    public function guildList(Request $request)
    {
        $data = $request->postData;
        $uuid = $data['uuid'] ?? '';
        if (empty(($uuid))) {
            return outJson(1, '玩家id为空');
        }

        $all_guilds = Guild::query()->orderByDesc('contribution')->limit(50)->get()->toArray();

        $lists = [];
        $rank = 0;
        foreach ($all_guilds as $all_guild) {
            $rank = $rank + 1;
            $lists[] = [
                'id' => $all_guild['id'],
                'name' => $all_guild['name'],
                'contribution' => $all_guild['contribution'],
                'rank' => $rank,
            ];
        }
        $out_data['list'] = $lists;
        return outJson(0, 'ok', $out_data);
    }

    /***
     * 公会信息查询
     * @param Request $request
     * @return Response
     */
    public function guildInfo(Request $request)
    {
        $data = $request->postData;
        // print_r($data);
        $uuid = $data['uuid'] ?? '';
        $role_id = $data['role_id'] ?? 0;
        if (empty(($uuid))) {
            return outJson(1, '玩家id为空');
        }
        if (empty(($role_id))) {
            return outJson(1, '角色id为空');
        }

        //查询是否加入公会
        $where_cond['uuid'] = $uuid;
        $where_cond['role_id'] = $role_id;
        $guild_member_info = GuildMember::query()->where($where_cond)->first();
        $guild_member_info = $guild_member_info ? $guild_member_info->toArray() : [];
        $guild_id = $guild_member_info['guild_id'] ?? 0;
        if (empty($guild_id)) {
            return outJson(1, '还未加入公会');
        }
        //查询公会信息
        $guild_info = Guild::query()->where('id', $guild_id)->first()->toArray();

        //获取用户昵称
        $user_info = User::query()->where('uuid', $guild_info['uuid'])->first();
        $user_info = $user_info ? $user_info->toArray() : [];

        //角色数据
        $where_cond = [];
        $where_cond['role_id'] = $role_id;
        $where_cond['uuid'] = $uuid;
        $my_fight_cap = Role::query()->where($where_cond)->value('fight_cap');

        //获取公会总人数
        $guildMembers = GuildMember::query()->where('guild_id', $guild_id)->get();
        $guildMembers = $guildMembers ? $guildMembers->toArray() : [];
        $now_num = count($guildMembers);

        //回去工会所有会员
        $total_fight_cap = 0;
        foreach ($guildMembers as $item) {
            $where_cond = [];
            $where_cond['role_id'] = $item['role_id'];
            $where_cond['uuid'] = $item['uuid'];
            $role_info = Role::query()->where($where_cond)->first();
            $role_info = $role_info ? $role_info->toArray() : [];
            $total_fight_cap = $total_fight_cap + ($role_info['fight_cap'] ?? 0);
        }

        //获取公会科技等级
        $g_techs = GuildTechnology::query()->where('guild_id', $guild_id)->get();
        $g_techs = $g_techs ? $g_techs->toArray() : [];
        $_g_techs = [];
        foreach ($g_techs as $g_tech) {
            $_g_techs[] = [
                'skill_id' => $g_tech['skill_id'],
                'level' => $g_tech['level'],
                'name' => Constant::TECHNOLOGYS[$g_tech['skill_id']],
            ];
        }

        $out_data = [];
        $out_data['name'] = $guild_info['name'];
        $out_data['president'] = $user_info['nickname'] ?? '';
        $out_data['grade'] = $guild_info['grade'];
        $out_data['id'] = $guild_info['id'];
        $out_data['now_num'] = $now_num;
        // $out_data['total_num'] = Constant::GUILD_GRADE_PEOPLE_NUM[$guild_info['grade']];
        $out_data['total_num'] = (new GuildLevelUp())->getPeopleLimitNum($guild_info['grade']);
        $out_data['created_at'] = $guild_info['created_at'];
        $out_data['fight_cap'] = $total_fight_cap;
        $out_data['notice'] = $guild_info['notice'];
        $out_data['my_job'] = Constant::GUILD_JOBS[$guild_member_info['job']];
        $out_data['designation'] = '';
        $out_data['mem_id'] = $guild_member_info['id'] ?? 0;
        $out_data['my_fight_cap'] = $my_fight_cap ?? 0;
        $out_data['contribution'] = $guild_member_info['contribution'] ?? 0;
        $out_data['tech'] = $_g_techs;
        return outJson(0, 'ok', $out_data);
    }

    /***
     * 修改公告
     * @param Request $request
     * @return Response
     */
    public function updateNotice(Request $request)
    {
        $data = $request->postData;
        // print_r($data);
        $guild_id = $data['guild_id'] ?? 0;
        $notice = $data['notice'] ?? '';
        $uuid = $data['uuid'] ?? '';
        $role_id = $data['role_id'] ?? 0;
        if (empty(($guild_id))) {
            return outJson(1, '参数错误');
        }

        //查询是否加入公会
        $guild_member_info = GuildMember::query()->where('uuid', $uuid)->where('role_id', $role_id)->first();
        $guild_member_info = $guild_member_info ? $guild_member_info->toArray() : [];
        if (empty($guild_member_info)) {
            return outJson(1, '还未加入公会');
        }

        if ($guild_member_info['job'] > 2) {
            return outJson(1, '权限不足');
        }
        //查询公会信息
        $guild_info = Guild::query()->where('id', $guild_id)->first();
        if (empty($guild_info)) {
            return outJson(1, '工会不存在');
        }

        $guild_info->notice = $notice;
        $guild_info->save();

        return outJson(0, 'ok');
    }


    /***
     * 成员列表
     * @param Request $request
     * @return Response
     */
    public function memList(Request $request)
    {
        $data = $request->postData;
        // print_r($data);
        $guild_id = $data['guild_id'] ?? 0;
        if (empty(($guild_id))) {
            return outJson(1, '参数为空');
        }

        $GuildMembers = GuildMember::query()->where('guild_id', $guild_id)->orderByDesc('job')->get();
        $GuildMembers = $GuildMembers ? $GuildMembers->toArray() : [];
        $user_ids = array_column($GuildMembers, 'uuid');

        $users = User::query()->pluck('nickname', 'uuid');
        $users = $users ? $users->toArray() : [];

        $roles = Role::query()->whereIn('uuid', $user_ids)->get();
        $roles = $roles ? $roles->toArray() : [];
        $_roles = [];
        foreach ($roles as $role) {
            $_roles[$role['uuid']][$role['role_id']] = $role;
        }

        // var_dump($user_ids, $roles);

        $lists = [];
        foreach ($GuildMembers as $GuildMember) {
            $lists[] = [
                'id' => $GuildMember['id'],
                'role_name' => $users[$GuildMember['uuid']] ?? '',
                'job' => Constant::GUILD_JOBS[$GuildMember['job']],
                'contribution' => $GuildMember['contribution'],//贡献
                'designation' => $GuildMember['designation'],//称号
                'fight_cap' => $_roles[$GuildMember['uuid']][$GuildMember['role_id']]['fight_cap'] ?? 0,//战力
            ];
        }
        $out_data['list'] = $lists;
        return outJson(0, 'ok', $out_data);
    }

    /***
     * 邀请加入
     * @param Request $request
     * @return Response
     */
    public function inviteJoin(Request $request)
    {
        $data = $request->postData;
        // print_r($data);
        $guild_id = $data['guild_id'] ?? '';//邀请者id
        $uuid = $data['uuid'] ?? '';//邀请者角色id
        $nickname = $data['nickname'] ?? 0;//被邀请者昵称
        if (empty(($guild_id))) {
            return outJson(1, '参数为空');
        }
        if (empty(($nickname))) {
            return outJson(1, '参数为空');
        }

        //判断公会是否满
        $guild_info = Guild::query()->where('id', $guild_id)->first();
        $guild_info = $guild_info ? $guild_info->toArray() : [];
        $guild_grade = intval($guild_info['grade']);
        // $total_num = Constant::GUILD_GRADE_PEOPLE_NUM[$guild_grade];
        $total_num = (new GuildLevelUp())->getPeopleLimitNum($guild_grade);
        $now_num = GuildMember::query()->where('guild_id', $guild_id)->count();
        if ($now_num >= $total_num) {
            return outJson(1, '公会已满员');
        }

        $user_info = User::query()->where('nickname', $nickname)->first();
        if (empty($user_info)) {
            return outJson(1, '用户不存在');
        }

        $invite_nickname = User::query()->where('uuid', $uuid)->value('nickname');

        for ($i = 1; $i < 8; $i++) {
            $mail_model = new Mail();
            $mail_model->type = Constant::MAIL_TYPE_1;
            $mail_model->uuid = $user_info['uuid'];
            $mail_model->role_id = $i;
            $mail_model->title = '邀请加入公会';
            $mail_model->content = sprintf("玩家%s邀请你加入%s", $invite_nickname, $guild_info['name']);
            $mail_model->ext = ['guild_id' => intval($guild_id)];
            $mail_model->save();

            $real_uuid = $user_info['uuid'] . '-' . $i;
            Gateway::sendToUid($real_uuid, json_encode([
                'type' => MsgType::TYPE_33,
                'email' => 1,
            ]));

        }

        return outJson(0, 'ok');
    }

    /***
     * 加入公会
     * @param Request $request
     * @return Response
     */
    public function doJoin(Request $request)
    {
        $data = $request->postData;
        // print_r($data);
        $guild_id = $data['guild_id'] ?? '';//邀请者id
        $uuid = $data['uuid'] ?? '';//邀请者角色id
        $role_id = $data['role_id'] ?? 0;//被邀请者昵称
        if (empty(($guild_id))) {
            return outJson(1, '参数为空');
        }
        if (empty(($uuid))) {
            return outJson(1, '参数为空');
        }
        if (empty(($role_id))) {
            return outJson(1, '参数为空');
        }

        //判断是否在公会
        $where_cond['uuid'] = $uuid;
        $where_cond['role_id'] = $role_id;
        $where_cond['guild_id'] = $guild_id;
        $is_join = GuildMember::query()->where($where_cond)->exists();
        if ($is_join) {
            return outJson(1, '已加入公会');
        }

        //判断公会是否满
        $guild_info = Guild::query()->where('id', $guild_id)->first();
        $guild_info = $guild_info ? $guild_info->toArray() : [];
        $guild_grade = $guild_info['grade'] ?? 1;
        // $total_num = Constant::GUILD_GRADE_PEOPLE_NUM[$guild_grade];
        $total_num = (new GuildLevelUp())->getPeopleLimitNum($guild_grade);
        $now_num = GuildMember::query()->where('guild_id', $guild_id)->count();
        if ($now_num >= $total_num) {
            return outJson(1, '公会已满员');
        }

        $GuildMember = new  GuildMember();
        $GuildMember->guild_id = $guild_id;
        $GuildMember->uuid = $uuid;
        $GuildMember->job = Constant::JOB_5;
        $GuildMember->role_id = $role_id;
        $GuildMember->save();

        $nickname = User::query()->where('uuid', $uuid)->value('nickname');
        Gateway::sendToGroup($guild_id, json_encode([
            'type' => MsgType::TYPE_3,
            'nickname' => Constant::SYSTEM_NAME,
            'content' => sprintf('欢迎%s加入公会', $nickname),
            'created_at' => date('Y-m-d H:i:s'),
        ], JSON_UNESCAPED_UNICODE));

        return outJson(0, 'ok');
    }

    /***
     * 主动退出公会
     * @param Request $request
     * @return Response
     */
    public function exit(Request $request)
    {
        $data = $request->postData;
        $guild_id = $data['guild_id'] ?? '';//邀请者id
        $uuid = $data['uuid'] ?? '';//邀请者角色id
        $role_id = $data['role_id'] ?? 0;//被邀请者昵称
        if (empty(($guild_id))) {
            return outJson(1, '参数为空');
        }
        if (empty(($uuid))) {
            return outJson(1, '参数为空');
        }
        if (empty(($role_id))) {
            return outJson(1, '参数为空');
        }

        //判断是否在公会
        $where_cond['uuid'] = $uuid;
        $where_cond['role_id'] = $role_id;
        $where_cond['guild_id'] = $guild_id;
        $GuildMember = GuildMember::query()->where($where_cond)->first();
        if (empty($GuildMember)) {
            return outJson(1, '不在公会中');
        }
        $GuildMember->delete();

        $nickname = User::query()->where('uuid', $uuid)->value('nickname');
        Gateway::sendToGroup($guild_id, json_encode([
            'type' => MsgType::TYPE_3,
            'nickname' => Constant::SYSTEM_NAME,
            'content' => sprintf('%s退出公会', $nickname),
            'created_at' => date('Y-m-d H:i:s'),
        ], JSON_UNESCAPED_UNICODE));

        return outJson(0, 'ok');
    }


    /***
     * 踢出公会
     * @param Request $request
     * @return Response
     */
    public function kickOut(Request $request)
    {
        $data = $request->postData;
        $uuid = $data['uuid'] ?? '';//
        $role_id = $data['role_id'] ?? 0;//
        $mem_id = $data['mem_id'] ?? 0;
        var_dump($uuid, $role_id, $mem_id);
        if (empty(($mem_id))) {
            return outJson(1, '参数为空');
        }
        if (empty(($uuid))) {
            return outJson(1, '参数为空');
        }
        if (empty(($role_id))) {
            return outJson(1, '参数为空');
        }

        $where_cond['uuid'] = $uuid;
        $where_cond['role_id'] = $role_id;
        $GuildMember = GuildMember::query()->where($where_cond)->first();
        if (empty($GuildMember)) {
            return outJson(1, '成员不存在');
        }

        if ($GuildMember['id'] == $mem_id) {
            return outJson(1, '不能对自己操作');
        }

        $GuildMember2 = GuildMember::query()->where('id', $mem_id)->first();
        if ($GuildMember['id'] == $mem_id) {
            return outJson(1, '参数为空');
        }
        if ($GuildMember['job'] >= $GuildMember2['job']) {
            return outJson(1, '权限不足');

        }
        $nickname = User::query()->where('uuid', $GuildMember2['uuid'])->value('nickname');
        Gateway::sendToGroup($GuildMember2['guild_id'], json_encode([
            'type' => MsgType::TYPE_3,
            'nickname' => Constant::SYSTEM_NAME,
            'content' => sprintf('%s被踢出了公会', $nickname),
            'created_at' => date('Y-m-d H:i:s'),
        ], JSON_UNESCAPED_UNICODE));

        $real_uuid = $GuildMember2['uuid'] . '-' . $GuildMember2['role_id'];
        var_dump($real_uuid, [
            'type' => MsgType::TYPE_32,
            'uuid' => $GuildMember2['uuid'],
            'role_id' => $GuildMember2['role_id'],
        ]);
        Gateway::sendToUid($real_uuid, json_encode([
            'type' => MsgType::TYPE_32,
            'uuid' => $GuildMember2['uuid'],
            'role_id' => $GuildMember2['role_id'],
        ]));

        $GuildMember2->delete();

        return outJson(0, 'ok');
    }

    // /***
    //  * 判断是否可以设置职位
    //  * @param $myjob 操作者当前职位
    //  * @param $t_job 被作者当前职位
    //  * @param $new_job 被赋予新的职位
    //  * @return true|void
    //  */
    // private function checkCanSet($myjob, $t_job, $new_job)
    // {
    //     if ($myjob == 1) {
    //         return true;
    //     }
    //
    //     if ($myjob == 2) {
    //         $GuildMember = GuildMember::query()->where('id', $mem_id)->first();
    //         $GuildMember = $GuildMember ? $GuildMember->toArray() : [];
    //         $they_job = $GuildMember['job'];
    //     }
    // }

    /***
     * 任命职位
     * @param Request $request
     * @return Response
     */
    public function setPosition(Request $request)
    {
        $data = $request->postData;
        $uuid = $data['uuid'] ?? '';
        $role_id = $data['role_id'] ?? 0;
        $mem_id = $data['mem_id'] ?? 0;
        $new_job = $data['job'] ?? 0;
        if (empty($uuid) || empty($role_id) || empty($new_job)) {
            return outJson(1, '参数为空');
        }
        if (!in_array($new_job, [Constant::JOB_1, Constant::JOB_2, Constant::JOB_3, Constant::JOB_4, Constant::JOB_5])) {
            return outJson(1, '参数错误');
        }
        $where_cond['uuid'] = $uuid;
        $where_cond['role_id'] = $role_id;
        $GuildMember = GuildMember::query()->where($where_cond)->first();
        $GuildMember = $GuildMember ? $GuildMember->toArray() : [];

        //判断职位数量是否超限
        $job_num = GuildMember::query()->where('guild_id', $GuildMember['guild_id'])->where('job', $new_job)->count();
        if (intval($job_num) >= Constant::GUILD_JOBS_NUM[$new_job]) {
            return outJson(1, '该职位成员数量超过限制');
        }

        if ($GuildMember['job'] == Constant::JOB_2) {
            if ($new_job < Constant::JOB_3) {
                return outJson(1, '没有权限操作');
            }
        }
        if ($GuildMember['job'] > Constant::JOB_2) {
            return outJson(1, '没有权限操作');
        }

        //判断是否在公会
        $GuildMember_1 = GuildMember::query()->where('id', $mem_id)->first();
        if (empty($GuildMember_1)) {
            return outJson(1, '数据错误');
        }
        $GuildMember_1->job = $new_job;
        $GuildMember_1->save();

        $nickname = User::query()->where('uuid', $GuildMember_1['uuid'])->value('nickname');
        Gateway::sendToGroup($GuildMember_1['guild_id'], json_encode([
            'type' => MsgType::TYPE_3,
            'nickname' => Constant::SYSTEM_NAME,
            'content' => sprintf('%s被任命为%s', $nickname, Constant::GUILD_JOBS[$new_job] ?? ''),
            'created_at' => date('Y-m-d H:i:s'),
        ], JSON_UNESCAPED_UNICODE));

        return outJson(0, 'ok');
    }

    /***
     * 工会升级
     * @param Request $request
     * @return Response
     */
    public function levelUp(Request $request)
    {
        $data = $request->postData;
        $guild = $data['guild_id'] ?? '';
        $uuid = $data['uuid'] ?? '';
        $role_id = $data['role_id'] ?? 0;
        // var_dump($guild,$uuid,$role_id);
        if (empty($guild) || empty($uuid) || empty($role_id)) {
            return outJson(1, '参数为空1');
        }

        //公会数据
        $guild_info = Guild::query()->where('id', $guild)->first();
        if (empty($guild_info)) {
            return outJson(1, '配置错误');
        }

        $guild_level_model = new GuildLevelUp();
        if ($guild_info['grade'] >=
            $guild_level_model->getMaxLevel()) {
            return outJson(1, '已达最大等级');
        }

        $cond_where = ['uuid' => $uuid, 'role_id' =>
            $role_id, 'guild_id' => $guild];
        $GuildMember = GuildMember::query()->where($cond_where)->first();
        if (empty($GuildMember)) {
            return outJson(1, '配置错误');
        }

        //判断职位
        if ($GuildMember['job'] > Constant::JOB_4) {
            return outJson(1, '需要会长或副会长进行操作');
        }

        $next_level = $guild_info['grade'] + 1;
        $guild_level_config = $guild_level_model->getLevelConfig($next_level);

        //计算公会总战力
        $total_fight_cap = (new GuildMember())->getGuildFightCap($guild);
        if ($total_fight_cap < $guild_level_config['need_fight_cap']) {
            return outJson(1, '公会战力不足');
        }

        if ($guild_info['contribution'] < $guild_level_config['cost_contribution']) {
            return outJson(1, '公会贡献不足');
        }

        $guild_info->increment('grade', $next_level);
        $guild_info->decrement('contribution', $guild_level_config['cost_contribution']);
        $guild_info->save();

        Gateway::sendToGroup($guild_info['id'], json_encode([
            'type' => MsgType::TYPE_3,
            'nickname' => Constant::SYSTEM_NAME,
            'content' => sprintf('公会升级到%s级', $next_level),
            'created_at' => date('Y-m-d H:i:s'),
        ], JSON_UNESCAPED_UNICODE));

        return outJson(0, 'ok');
    }

    /***
     * 完成任务
     * @param Request $request
     * @return Response
     */
    public function completeTask(Request $request)
    {
        $data = $request->postData;
        $uuid = $data['uuid'] ?? '';
        $role_id = $data['role_id'] ?? 0;
        $task_id = $data['task_id'] ?? 0;
        if (empty($uuid) || empty($role_id) || empty($task_id)) {
            return outJson(1, '参数为空1');
        }

        //获取所在公会
        $guild_id = (new GuildMember())->getGuildByUuidRoleID($uuid, $role_id);
        if (empty($guild_id)) {
            return outJson(1, '还未加入公会');
        }

        $guild_tasks = GuildTask::getSingleByKey($task_id);
        if (empty($guild_tasks)) {
            return outJson(1, '任务不存在');
        }

        //判断之前领取过没有
        $cond_where = ['uuid' => $uuid, 'role_id' => $role_id, 'task_id' => $task_id];
        $is_exs = GuildTaskLog::query()->where($cond_where)->exists();
        if ($is_exs) {
            return outJson(1, '已经领取过了');
        }

        echo $guild_tasks['任务奖励道具'];
        $awards = json_decode($guild_tasks['任务奖励道具'], true);
        if (empty($awards)) {
            return outJson(1, '奖励配置错误');
        }

        $guild_award_num = 0;
        foreach ($awards as $award) {
            //公会贡献
            if ($award['id'] == 3) {
                $guild_model = Guild::where('id', $guild_id)->first();
                $guild_model->increment('contribution', $award['num']);
                $guild_award_num = $award['num'];
            }
            //个人贡献
            if ($award['id'] == 4) {
                $guildMember_model = GuildMember::query()->where('uuid', $uuid)->where('role_id', $role_id)->first();
                $guildMember_model->increment('contribution', $award['num']);
            }
        }

        //记录任务完成日志
        GuildTaskLog::create([
            'task_id' => $task_id,
            'uuid' => $uuid,
            'role_id' => $role_id,
        ]);

        $nickname = User::query()->where('uuid', $uuid)->value('nickname');
        Gateway::sendToGroup($guild_id, json_encode([
            'type' => MsgType::TYPE_3,
            'nickname' => Constant::SYSTEM_NAME,
            'content' => sprintf('%s完成了公会任务，为公会增加了，%s公会贡献', $nickname, $guild_award_num),
            'created_at' => date('Y-m-d H:i:s'),
        ], JSON_UNESCAPED_UNICODE));

        return outJson(0, 'ok');
    }

    /***
     * 修改称号
     * @param Request $request
     * @return Response
     */
    public function updateDesignation(Request $request)
    {
        $data = $request->postData;
        $uuid = $data['uuid'] ?? '';
        $role_id = $data['role_id'] ?? 0;
        $mem_id = $data['mem_id'] ?? 0;
        $designation = $data['designation'] ?? 0;
        if (empty($uuid) || empty($role_id) || empty($mem_id)) {
            return outJson(1, '参数为空');
        }

        $where_cond['uuid'] = $uuid;
        $where_cond['role_id'] = $role_id;
        $GuildMember = GuildMember::query()->where($where_cond)->first();
        if (empty($GuildMember)) {
            return outJson(1, '数据错误');
        }

        if ($GuildMember['job'] > Constant::JOB_2) {
            return outJson(1, '没有权限操作');
        }
        //判断是否在公会
        $GuildMember_1 = GuildMember::query()->where('id', $mem_id)->first();
        if (empty($GuildMember_1)) {
            return outJson(1, '数据错误');
        }

        if ($GuildMember_1['guild_id'] != $GuildMember['guild_id']) {
            return outJson(1, '不在同一公会');
        }
        $GuildMember_1->designation = $designation;
        $GuildMember_1->save();
        return outJson(0, 'ok');
    }

    /***
     * 任命会长
     * @param Request $request
     * @return Response
     */
    public function appointPresident(Request $request)
    {
        $data = $request->postData;
        $uuid = $data['uuid'] ?? '';
        $role_id = $data['role_id'] ?? 0;
        $mem_id = $data['mem_id'] ?? 0;
        if (empty($uuid) || empty($role_id) || empty($mem_id)) {
            return outJson(1, '参数为空');
        }

        $where_cond['uuid'] = $uuid;
        $where_cond['role_id'] = $role_id;
        $GuildMember = GuildMember::query()->where($where_cond)->first();
        if (empty($GuildMember)) {
            return outJson(1, '数据错误');
        }

        if ($GuildMember['job'] != Constant::JOB_1) {
            return outJson(1, '不是会长，无权操作');
        }
        //判断是否在公会
        $GuildMember_1 = GuildMember::query()->where('id', $mem_id)->first();
        if (empty($GuildMember_1)) {
            return outJson(1, '数据错误');
        }

        if ($GuildMember_1['guild_id'] != $GuildMember['guild_id']) {
            return outJson(1, '不在同一公会');
        }
        $GuildMember_1->job = Constant::JOB_1;
        $GuildMember_1->save();


        $GuildMember->job = Constant::JOB_5;
        $GuildMember->save();
        return outJson(0, 'ok');
    }

    /***
     * 公会科技升级
     * @param Request $request
     * @return Response
     */
    public function doLevelUpTech(Request $request)
    {
        $data = $request->postData;
        $uuid = $data['uuid'] ?? '';
        $role_id = $data['role_id'] ?? 0;
        $skill_id = $data['skill_id'] ?? 0;

        $where_cond['uuid'] = $uuid;
        $where_cond['role_id'] = $role_id;
        $GuildMember = GuildMember::query()->where($where_cond)->first();
        if (empty($GuildMember)) {
            return outJson(1, '数据错误1');
        }

        $guild_id = $GuildMember['guild_id'];
        echo $guild_id;
        // 判断资源
        $guild_model = Guild::query()->where('id', $guild_id)->first();
        if (empty($guild_model)) {
            return outJson(1, '数据错误2');
        }

        if (!in_array($GuildMember['job'], [Constant::JOB_1, Constant::JOB_2])) {
            return outJson(1, '不是会长，无权操作');
        }

        //判断公会贡献是否足够
        $tech_model = GuildTechnology::query()->where('guild_id', $guild_id)->where('skill_id', $skill_id)->first();
        $now_level = $tech_model['level'] ?? 0;
        $next_level = $now_level + 1;
        if ($next_level > 5) {
            return outJson(1, '等级超过最大限制');
        }

        //获取消耗配置
        $g_conf = GuildTechConf::getConfBySkillIdAndLevel($skill_id, $next_level);
        if ($guild_model['contribution'] < $g_conf['升级消耗']) {
            return outJson(1, '公会贡献不足！');
        }

        $guild_model->decrement('contribution', $g_conf['升级消耗']);

        // 记录公会科技等级
        if (empty($tech_model)) {
            GuildTechnology::create([
                'guild_id' => $guild_id,
                'skill_id' => $skill_id,
                'level' => 1,
            ]);
        } else {
            $tech_model->increment('level', 1);
        }
        return outJson(0, 'ok');
    }


    /***
     * 商店购买
     * @param Request $request
     * @return Response
     */
    public function shopBuy(Request $request)
    {
        $data = $request->postData;
        $uuid = $data['uuid'] ?? '';
        $role_id = $data['role_id'] ?? 0;
        $item_key = $data['item_key'] ?? 0;

        $shop_item = Shop::getItemByItemKeyType($item_key);
        if (empty($shop_item)) {
            return outJson(1, '物品不存在');
        }

        $where_cond['uuid'] = $uuid;
        $where_cond['role_id'] = $role_id;
        $GuildMember = GuildMember::query()->where($where_cond)->first();
        if (empty($GuildMember)) {
            return outJson(1, '数据错误');
        }

        //判断个人贡献是否足够
        if ($GuildMember['contribution'] < $shop_item['价格']) {
            return outJson(1, '个人贡献不足！');
        }

        $GuildMember->decrement('contribution', $shop_item['价格']);

        return outJson(0, 'ok');
    }


}
